package tgc.edu.examxy.web.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.examxy.entity.CurriculumPlan;
import tgc.edu.examxy.entity.Student;
import tgc.edu.examxy.security.User2;
import tgc.edu.examxy.service.CurriculumPlanService;
import tgc.edu.examxy.service.StudentService;
import tgc.edu.examxy.web.form.CurriculumTableForm;
/****
 * 学生课表
 * @author 936642284
 *
 */
@Controller
@RequestMapping(value="/curriculum")
public class CurriculumController{
	@Autowired
	private CurriculumPlanService cservice;
	@Autowired
	private StudentService studentservice;
	/***
	 * 跳转至学生课表界面
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/manage")
	private String manage(ModelMap map) {
		Sort sort=Sort.by("starttime");
		Specification<CurriculumPlan> spec = buildSpec();
		List<CurriculumPlan> clist = cservice.findAll(spec, sort);
		List<CurriculumTableForm> findBydtkc = cservice.findBydtkc(clist);//处理相同的文字
		List<CurriculumTableForm> tableformlist = cservice.findTr(findBydtkc);//处理空白单元格
		map.put("model", tableformlist);
		return "curriculum/manage";
	}

	private Specification<CurriculumPlan> buildSpec() {
		SecurityContext ctx = SecurityContextHolder.getContext();
	    Authentication auth = ctx.getAuthentication();
	    User2 user = (User2) auth.getPrincipal();
	    Student usernames = studentservice.findByUsername(user.getUsername());
	    
		//java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis()); 
		Specification<CurriculumPlan> specification = new Specification<CurriculumPlan>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<CurriculumPlan> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(usernames!=null) {
					Predicate name = cb.equal(root.get("tbClass").get("id"), usernames.getTbClass().getId());
					rules.add(name);
				}
				//条件大于这个时间的所有课程
				/*Predicate name = cb.greaterThanOrEqualTo(root.get("starttime"), currentDate);
				rules.add(name);*/
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return specification;
	}
}
